/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: ShiroFactroy
 * Author:   zhouheng
 * Date:     2018/6/18 10:54
 * Description: Shiro工厂类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hengby.hengcrud.core.shiro.factory;

import com.hengby.hengcrud.core.common.factory.ConstantFactory;
import com.hengby.hengcrud.core.common.status.ManagerStatus;
import com.hengby.hengcrud.core.shiro.ShiroUser;
import com.hengby.hengcrud.core.util.Convert;
import com.hengby.hengcrud.core.util.SpringContextHold;
import com.hengby.hengcrud.modular.sys.mapper.MenuMapper;
import com.hengby.hengcrud.modular.sys.mapper.UserMapper;
import com.hengby.hengcrud.modular.sys.model.User;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈Shiro工厂类〉
 *
 * @author zhouheng
 * @create 2018/6/18
 * @since 1.0.0
 */
@Service
@DependsOn(value = "springContextHold")
@Transactional(readOnly = true)
public class ShiroFactroy implements IShiro{
    @Autowired
    UserMapper userMapper;

    @Autowired
    private MenuMapper menuMapper;

    public static IShiro me(){
        return SpringContextHold.getBean(IShiro.class);
    }

    @Override
    public User user(String account) {
        User user = userMapper.getUserByAccount(account);

        // 账号不存在
        if (null == user) {
            throw new CredentialsException();
        }
        // 账号被冻结
        if (user.getStatus() == ManagerStatus.DELETED.getCode()) {
            throw new LockedAccountException();
        }
        return user;
    }

    @Override
    public ShiroUser shiroUser(User user) {
        ShiroUser shiroUser = new ShiroUser();

        shiroUser.setId(user.getId());
        shiroUser.setAccount(user.getAccount());
        shiroUser.setDeptId(user.getDeptid());
        shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptid()));
        shiroUser.setName(user.getName());

        Integer[] roleArray = Convert.toIntArray(user.getRoleid());
        List<Integer> roleList = new ArrayList<Integer>();
        List<String> roleNameList = new ArrayList<String>();
        for (int roleId : roleArray) {
            roleList.add(roleId);
            roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
        }
        shiroUser.setRoleList(roleList);
        shiroUser.setRoleNames(roleNameList);

        return shiroUser;
    }

    @Override
    public List<String> findPermissionsByRoleId(Integer roleId) {
        return menuMapper.getResUrlsByRoleId(roleId);
    }

    @Override
    public String findRoleNameByRoleId(Integer roleId) {
        return ConstantFactory.me().getSingleRoleTip(roleId);
    }

    @Override
    public SimpleAuthenticationInfo info(ShiroUser shiroUser, User user, String realmName) {
        String password = user.getPassword();
        // 密码加盐处理
        String salt = user.getSalt();
//        ByteSource credentialsSalt = new Md5Hash(salt);
        ByteSource credentialsSalt = ByteSource.Util.bytes(salt);

        return new SimpleAuthenticationInfo(shiroUser,password,credentialsSalt,realmName);
    }
}